<template>
  <el-breadcrumb :separator-icon="ArrowRight" >
    <el-breadcrumb-item to="/">首页</el-breadcrumb-item>
      <div>
        <el-breadcrumb-item v-for="item in category" :key="item.id"  :to="item.path"  >{{item.name}}</el-breadcrumb-item>
      </div>
  </el-breadcrumb>
</template>

<script>
import { useStore } from 'vuex'
import { useRoute } from 'vue-router'
import { computed, ref } from 'vue'
import { ArrowRight } from '@element-plus/icons-vue'

export default {
  name: 'XtxBreadcrumb',
  setup (props) {
    const route = useRoute()
    const store = useStore()
    const category = computed(() => {
      const arr = []
      store.state.category.list.forEach(top => {
        // 一级
        if(top.id === route.params.id){
            arr.push({ id: top.id, name: top.name, path:`/category/${top.id}`})
            return
        }
        // 二级
        top.children && top.children.forEach(sub => {
          if(sub.id === route.params.id){
            arr.push({ id: top.id, name: top.name, path:`/category/${top.id}`})
            arr.push({ id: sub.id, name: sub.name, path:`/category/sub/${sub.id}`})
            return
          }
        })
      })
      return arr
    })
    return { category, ArrowRight }
  }
}
</script>

<style lang="scss" scoped>
.el-breadcrumb {
    color: #666;
    font-weight: normal;
    text-align: center;
    line-height: 70px;
  }
</style>